home *** CD-ROM | disk | FTP | other *** search
/ CU Amiga Super CD-ROM 18 / CU Amiga Magazine's Super CD-ROM 18 (1997)(EMAP Images)(GB)[!][issue 1998-01].iso / CUCD / Programming / AmigaE / Src / Rkrm / Graphics_Libraries / Primitives / RGBBoxes.e < prev    next >
Encoding:
Text File  |  1995-09-20  |  6.8 KB  |  200 lines

  1. -> RGBBoxes.e - Simple ViewPort example -- works with 1.3 and Release 2
  2.  
  3. ->>> Header (globals)
  4. MODULE 'dos/dos',
  5.        'exec/libraries',
  6.        'graphics/displayinfo',
  7.        'graphics/gfx',
  8.        'graphics/gfxbase',
  9.        'graphics/gfxnodes',
  10.        'graphics/modeid',
  11.        'graphics/videocontrol',
  12.        'graphics/view'
  13.  
  14. ENUM ERR_NONE, ERR_COLMAP, ERR_FINDDISP, ERR_GETDISP, ERR_GFXNEW, ERR_MONI,
  15.      ERR_RAST, ERR_VIDEO
  16.  
  17. RAISE ERR_COLMAP   IF GetColorMap()=NIL,
  18.       ERR_FINDDISP IF FindDisplayInfo()=NIL,
  19.       ERR_GETDISP  IF GetDisplayInfoData()=0,
  20.       ERR_GFXNEW   IF GfxNew()=NIL,
  21.       ERR_MONI     IF OpenMonitor()=NIL,
  22.       ERR_RAST     IF AllocRaster()=NIL,
  23.       ERR_VIDEO    IF VideoControl()<>NIL
  24.  
  25. -> The number of bitplanes, and the nominal width and height used in 1.3.
  26. CONST DEPTH=2, WIDTH=640, HEIGHT=400
  27.  
  28. -> RGB values for the four colours used
  29. CONST BLACK=$000, RED=$f00, GREEN=$0f0, BLUE=$00f
  30.  
  31. DEF bitMap:bitmap,
  32.     displaymem=NIL  -> Pointer for writing to BitMap memory
  33. ->>>
  34.  
  35. ->>> PROC main()
  36. PROC main() HANDLE
  37.   -> E-Note: a lot of the globals are really local to main()
  38.   DEF view:view, oldview=NIL, viewPort:viewport, cm=NIL:PTR TO colormap,
  39.       vextra=NIL:PTR TO viewextra, vpextra=NIL:PTR TO viewportextra,
  40.       monspec=NIL, dimquery:dimensioninfo, depth, box, rasInfo:rasinfo,
  41.       modeID, colortable, boxoffsets:PTR TO INT, gfx:PTR TO gfxbase
  42.   gfx:=gfxbase  -> E-Note: get the right type for gfxbase
  43.  
  44.   -> Set the plane pointers to NIL so the handler will know if they are used.
  45.   -> E-Note: this needs to be done *before* anything that may go wrong
  46.   FOR depth:=0 TO DEPTH-1 DO bitMap.planes[depth]:=NIL
  47.  
  48.   -> Example steals the screen from Intuition if Intuition is around.
  49.   oldview:=gfx.actiview  -> Save current View to restore later.
  50.  
  51.   InitView(view)  -> Initialise the View and set view.modes.
  52.   -> This is the old 1.3 way (only V_LACE counts).
  53.   view.modes:=view.modes OR V_LACE
  54.  
  55.   IF gfx.lib.version>=36
  56.     -> Form the ModeID from values in 'graphics/displayinfo'
  57.     modeID:=DEFAULT_MONITOR_ID OR HIRESLACE_KEY
  58.  
  59.     -> Make the viewextra object
  60.     vextra:=GfxNew(VIEW_EXTRA_TYPE)
  61.     -> Attach the ViewExtra to the View
  62.     GfxAssociate(view, vextra)
  63.     view.modes:=view.modes OR EXTEND_VSTRUCT
  64.  
  65.     -> Create and attach a MonitorSpec to the ViewExtra
  66.     monspec:=OpenMonitor(NIL, modeID)
  67.     vextra.monitor:=monspec
  68.   ENDIF
  69.  
  70.   -> Initialise the BitMap for RasInfo.
  71.   InitBitMap(bitMap, DEPTH, WIDTH, HEIGHT)
  72.  
  73.   -> Allocate space for BitMap
  74.   FOR depth:=0 TO DEPTH-1 DO bitMap.planes[depth]:=AllocRaster(WIDTH, HEIGHT)
  75.  
  76.   -> Initialise the RasInfo.
  77.   rasInfo:=[NIL, bitMap, 0, 0]:rasinfo
  78.  
  79.   InitVPort(viewPort)      -> Initialise the ViewPort.
  80.   view.viewport:=viewPort  -> Link the ViewPort into the View.
  81.   viewPort.rasinfo:=rasInfo
  82.   viewPort.dwidth:=WIDTH
  83.   viewPort.dheight:=HEIGHT
  84.  
  85.   -> Set the display mode the old-fashioned way
  86.   viewPort.modes:=V_HIRES OR V_LACE
  87.  
  88.   IF gfx.lib.version>=36
  89.     -> Make a ViewPortExtra and get ready to attach it
  90.     vpextra:=GfxNew(VIEWPORT_EXTRA_TYPE)
  91.  
  92.     -> Initialise the DisplayClip field of the ViewPortExtra
  93.     GetDisplayInfoData(NIL, dimquery, SIZEOF dimensioninfo, DTAG_DIMS, modeID)
  94.     CopyMem(dimquery.nominal, vpextra.displayclip, SIZEOF rectangle)
  95.     -> E-Note: FindDisplayInfo in a the tag-list later
  96.  
  97.     -> This is for backwards compatibility with, for example, a 1.3 screen
  98.     -> saver utility that looks at the Modes field.
  99.     viewPort.modes:=modeID AND $0000FFFF
  100.   ENDIF
  101.  
  102.   -> Initialize the ColorMap.
  103.   -> 2 planes deep, so 4 entries (2 raised to the #_planes power).
  104.   cm:=GetColorMap(4)
  105.  
  106.   IF gfx.lib.version>=36
  107.     -> Attach the color map and Release 2 extended structures
  108.     VideoControl(cm, [VTAG_ATTACH_CM_SET, viewPort,
  109.                       VTAG_VIEWPORTEXTRA_SET, vpextra,
  110.                       VTAG_NORMAL_DISP_SET, FindDisplayInfo(modeID),
  111.                       NIL])
  112.   ELSE
  113.     -> Attach the ColorMap, old 1.3-style
  114.     viewPort.colormap:=cm
  115.   ENDIF
  116.  
  117.   colortable:=[BLACK, RED, GREEN, BLUE]:INT
  118.   -> Change colors to those in colortable.
  119.   LoadRGB4(viewPort, colortable, 4)
  120.  
  121.   MakeVPort(view, viewPort)  -> Construct preliminary Copper instruction list.
  122.  
  123.   -> Merge preliminary lists into a real Copper list in the view object
  124.   MrgCop(view)
  125.  
  126.   -> Clear the ViewPort
  127.   FOR depth:=0 TO DEPTH-1
  128.     displaymem:=bitMap.planes[depth]
  129.     BltClear(displaymem, bitMap.bytesperrow*bitMap.rows, 1)
  130.   ENDFOR
  131.  
  132.   LoadView(view)
  133.  
  134.   boxoffsets:=[802, 2010, 3218]:INT
  135.   -> Now fill some boxes so that user can see something.
  136.   -> Always draw into both planes to assure true colors.
  137.   FOR box:=1 TO 3  -> Three boxes; red, green and blue.
  138.     FOR depth:=0 TO DEPTH-1  -> Two planes
  139.       displaymem:=bitMap.planes[depth]+boxoffsets[box-1]
  140.       drawFilledBox(box, depth)
  141.     ENDFOR
  142.   ENDFOR
  143.  
  144.   Delay(10*TICKS_PER_SECOND)  -> Pause for 10 seconds.
  145.   LoadView(oldview)           -> Put back the old View.
  146.   WaitTOF()  -> Wait until the View is being rendered to free memory.
  147.  
  148.   -> Deallocate the hardware Copper list created by MrgCopy().  Since this is
  149.   -> interlace, also check for a short frame copper list to free.
  150.   FreeCprList(view.lofcprlist)
  151.   IF view.shfcprlist THEN FreeCprList(view.shfcprlist)
  152.  
  153.   -> Free all intermediate Copper lists created by MakeVPort().
  154.   FreeVPortCopLists(viewPort)
  155.  
  156. EXCEPT DO
  157.   -> Free the color map created by GetColorMap()
  158.   IF cm THEN FreeColorMap(cm)
  159.   -> Free the ViewPortExtra created by GfxNew()
  160.   IF vpextra THEN GfxFree(vpextra)
  161.   -> Free the BitPlanes drawing area.
  162.   FOR depth:=0 TO DEPTH-1
  163.     IF bitMap.planes[depth] THEN FreeRaster(bitMap.planes[depth], WIDTH, HEIGHT)
  164.   ENDFOR
  165.   -> Free the MonitorSpec created with OpenMonitor().
  166.   IF monspec THEN CloseMonitor(monspec)
  167.   -> Free the ViewExtra created with GfxNew().
  168.   IF vextra THEN GfxFree(vextra)
  169.   SELECT exception
  170.   CASE ERR_COLMAP;    WriteF('Could not get ColorMap\n')
  171.   CASE ERR_FINDDISP;  WriteF('Could not get DisplayInfo\n')
  172.   CASE ERR_GETDISP;   WriteF('Could not get DimensionInfo\n')
  173.   CASE ERR_GFXNEW;    WriteF('Could not get ViewExtra/ViewPortExtra\n')
  174.   CASE ERR_MONI;      WriteF('Could not get MonitorSpec\n')
  175.   CASE ERR_RAST;      WriteF('Could not get BitPlanes\n')
  176.   CASE ERR_VIDEO;     WriteF('Could not attach extended structures\n')
  177.   ENDSELECT
  178. ENDPROC IF exception<>ERR_NONE THEN RETURN_FAIL ELSE RETURN_OK
  179. ->>>
  180.  
  181. ->>> PROC drawFilledBox(fillcolor, plane)
  182. -> Create a WIDTH/2 by HEIGHT/2 box of color "fillcolor" into the given plane
  183. PROC drawFilledBox(fillcolor, plane)
  184.   DEF value, boxHeight, boxWidth, width
  185.  
  186.   -> Divide (WIDTH/2) by eight because each CHAR that is written stuffs eight
  187.   -> bits into the BitMap.
  188.   boxWidth:=(WIDTH/2)/8
  189.  
  190.   value:=IF fillcolor AND Shl(1, plane) THEN $FF ELSE 0
  191.  
  192.   -> E-Note: slightly re-expressed to read a lot better...
  193.   FOR boxHeight:=1 TO HEIGHT/2
  194.     FOR width:=1 TO boxWidth DO displaymem[]++:=value
  195.     displaymem:=displaymem+(bitMap.bytesperrow-boxWidth)
  196.   ENDFOR
  197. ENDPROC
  198. ->>>
  199.  
  200.